package com.yylending.las.common.utils.area;

import android.content.Context;

import android.text.TextUtils;
import com.yyfax.common.CLog;
import com.yylending.las.common.utils.JsonMananger;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Title: LocalAreaManager Description: Company: YYLending(YouJingPuHui)
 *
 * @author XiongYuFeng
 *         date 2016年7月14日 上午9:16:58
 */
public class LocalAreaManager {
	public final static int PROVINCE_CITY_DISTRICT_FLAG = 1;//选择省市区
	public final static int PROVINCE_CITY_FLAG = 2;//选择省市
	public final static int DISTRICT_FLAG = 3;//选择区

	private final String TAG = LocalAreaManager.class.getSimpleName();

	private static LocalAreaManager mLocalAreaManager;

	public LocalAreaManager(Context context) {
		getAddressInfos(context);
		initProvinceCityDistrict();
	}

	public static LocalAreaManager getInstance(Context context) {
		if (null == mLocalAreaManager) {
			mLocalAreaManager = new LocalAreaManager(context);
		}
		return mLocalAreaManager;
	}

	public static LocalAreaManager getInstance() {
		return mLocalAreaManager;
	}

	public void clear(){
		mLocalAreaManager=null;
	}

	private String mAddressAssertFileName = "area.txt";

	public ArrayList<AddressProvinceBean> mProvinceList = new ArrayList<>();
	public ArrayList<ArrayList<AddressCityBean>> mCityList = new ArrayList<ArrayList<AddressCityBean>>();
	public ArrayList<ArrayList<ArrayList<AddressDistrictBean>>> mDistrictList = new ArrayList<ArrayList<ArrayList<AddressDistrictBean>>>();

	public int provinceIndex;
	public int cityIndex;
	public int districtIndex;

	public String province;
	public String city;
	public String district;

	/***
	 * 根据获取的省市 设定在省市区三个选项中 省市的索引
	 *
	 * @param province
	 * @param city
	 */
	public void setProvinceAndCityIndex(String province, String city) {
		this.province = province;
		this.city = city;
		for (int i = 0; i < mProvinceList.size(); i++) {
			AddressProvinceBean provinceBean = mProvinceList.get(i);
			if (provinceBean.getAreaName().equalsIgnoreCase(province)) {
				provinceIndex = i;
				break;
			}
		}

		for (int i = 0; i < mCityList.get(provinceIndex).size(); i++) {
			AddressCityBean addressCityBean = mCityList.get(provinceIndex).get(i);
			if (addressCityBean.getAreaName().equalsIgnoreCase(city)) {
				cityIndex = i;
				break;
			}
		}
		CLog.i(TAG, "provinceIndex" + provinceIndex + "cityIndex" + cityIndex);
	}


	/**根据获取的编号获取地址
	 *
	 * @param provinceId
	 * @param cityId
	 * @param districtId
	 * @return
	 */
	public String getProvinceAndCityName(String provinceId, String cityId, String districtId) {
		AddressProvinceBean myAddressProvinceBean = new AddressProvinceBean();
		AddressCityBean myAddressCityBean = new AddressCityBean();
		String myAddressDistrictName = "";
		for (int i = 0; i < mProvinceList.size(); i++) {
			AddressProvinceBean provinceBean = mProvinceList.get(i);
			if (provinceBean.getAreaId().equals(provinceId)) {
				myAddressProvinceBean = provinceBean;
				break;
			}
		}

		for (int i = 0; i < myAddressProvinceBean.getCities().size(); i++) {
			AddressCityBean addressCityBean = myAddressProvinceBean.getCities().get(i);
			if (addressCityBean.getAreaId().equals(cityId)) {
				myAddressCityBean = addressCityBean;
				break;
			}
		}
		if(!TextUtils.isEmpty(districtId)) {
			for (int i = 0; i < myAddressCityBean.getCounties().size(); i++) {
				AddressDistrictBean addressDistrictBean = myAddressCityBean.getCounties().get(i);
				if (addressDistrictBean.getAreaId().equals(districtId)) {
					myAddressDistrictName = addressDistrictBean.getAreaName();
					break;
				}
			}
		}
		return myAddressProvinceBean.getAreaName() + myAddressCityBean.getAreaName() + myAddressDistrictName;
	}

	/***
	 * 根据省市区查找省市区的索引值
	 * @param province
	 * @param city
	 * @param district
	 */
	public void setProvinceAndCityIndex(String province, String city, String district) {
		this.province = province;
		this.city = city;
		this.district = district;
		for (int i = 0; i < mProvinceList.size(); i++) {
			AddressProvinceBean provinceBean = mProvinceList.get(i);
			if (provinceBean.getAreaName().equalsIgnoreCase(province)) {
				provinceIndex = i;
				break;
			}
		}

		for (int i = 0; i < mCityList.get(provinceIndex).size(); i++) {
			AddressCityBean addressCityBean = mCityList.get(provinceIndex).get(i);
			if (addressCityBean.getAreaName().equalsIgnoreCase(city)) {
				cityIndex = i;
				break;
			}
		}
		for (int i = 0; i < mDistrictList.get(provinceIndex).get(cityIndex).size(); i++) {
			AddressDistrictBean districtBean = mDistrictList.get(provinceIndex).get(cityIndex).get(i);
			if (districtBean.getAreaName().equalsIgnoreCase(district)) {
				districtIndex = i;
				break;
			}

		}
		CLog.i(TAG, "provinceIndex" + provinceIndex + "cityIndex" + cityIndex +"districtIndex"+ districtIndex);
	}

	private void initProvinceCityDistrict() {
		for (AddressProvinceBean province : mProvinceList) {
			ArrayList<AddressCityBean> cityList = new ArrayList<AddressCityBean>();
			ArrayList<ArrayList<AddressDistrictBean>> districtList = new ArrayList<ArrayList<AddressDistrictBean>>();
			for (AddressCityBean city : province.getCities()) {
				cityList.add(city);
				ArrayList<AddressDistrictBean> districtList1 = new ArrayList<AddressDistrictBean>();
				for (AddressDistrictBean district : city.getCounties()) {
					districtList1.add(district);
				}
				districtList.add(districtList1);
			}
			this.mCityList.add(cityList);
			this.mDistrictList.add(districtList);
		}
	}

	private List<AddressProvinceBean> getAddressInfos(Context context) {
		String addressJsonStr = readLocalJson(context, this.mAddressAssertFileName);
		try {
			List<AddressProvinceBean> address = JsonMananger.jsonToList(addressJsonStr, AddressProvinceBean.class);
			mProvinceList.clear();
			mProvinceList.addAll(address);
		} catch (Exception e) {
		}
		return mProvinceList;
	}

	private String readLocalJson(Context context, String fileName) {
		String resultString = "";
		InputStream inputStream = null;
		try {
			inputStream = context.getResources().getAssets().open(fileName);
			byte[] buffer = new byte[inputStream.available()];
			inputStream.read(buffer);
			resultString = new String(buffer, "utf-8");
			inputStream.close();
		} catch (Exception e) {
		}
		return resultString;
	}
}
